package com.manage.competition.shiro;


import com.manage.competition.common.ServerResponse;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.*;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthorizedException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;

import javax.naming.AuthenticationException;

/**
 * Create with IDEA
 *
 * @Author:Vantcy
 * @Date: Create in 22:40 2019/1/29
 * @Description: 统一捕捉shiro的异常，返回给前台一个json信息，前台根据这个信息显示对应的提示，或者做页面的跳转。
 */
@RestControllerAdvice
@Slf4j
public class GlobalShiroExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler(UnauthorizedException.class)
    public ServerResponse unauthorizedException(UnauthorizedException e) {
        log.error(e.toString());
        return ServerResponse.createByErrorMessage("用户没有访问权限");
    }

    @ExceptionHandler(UnknownAccountException.class)
    public ServerResponse unknownAccountException(UnknownAccountException e) {
        log.error(e.toString());
        return ServerResponse.createByErrorMessage("用户不存在");
    }

    @ExceptionHandler(IncorrectCredentialsException.class)
    public ServerResponse incorrectCredentialsException(IncorrectCredentialsException e) {
        log.error(e.toString());
        return ServerResponse.createByErrorMessage(e.getMessage());
    }

    @ExceptionHandler(ExcessiveAttemptsException.class)
    public ServerResponse excessiveAttemptsException(ExcessiveAttemptsException e) {
        log.error(e.toString());
        return ServerResponse.createByErrorMessage(e.getMessage());
    }

    @ExceptionHandler(DisabledAccountException.class)
    public ServerResponse disabledAccountException(DisabledAccountException e) {
        log.error(e.toString());
        return ServerResponse.createByErrorMessage("该用户未激活");
    }

    @ExceptionHandler(LockedAccountException.class)
    public ServerResponse lockedAccountException(LockedAccountException e) {
        log.error(e.toString());
        return ServerResponse.createByErrorMessage("该用户被锁定，请联系管理员");
    }


    @ExceptionHandler(AuthenticationException.class)
    public ServerResponse authenticationException(AuthenticationException e) {
        log.error(e.toString());
        return ServerResponse.createByErrorMessage("身份认证失败");
    }

    @ExceptionHandler(AuthorizationException.class)
    public ServerResponse authorizationException(AuthorizationException e) {
        log.error(e.toString());
        return ServerResponse.createByErrorMessage("权限认证失败");
    }

    @ExceptionHandler(ShiroException.class)
    public ServerResponse shiroException(ShiroException e) {
        log.error(e.toString());
        return ServerResponse.createByErrorMessage("Shiro全局异常");
    }
}
